package net.zoneland.data.dataprofiling.common.service.impl;

import net.zoneland.data.dataprofiling.common.domain.DictEntity;
import net.zoneland.data.dataprofiling.common.domain.DictQuery;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.math.BigDecimal;
import java.util.Date;

public class DictSpecification implements Specification<DictEntity> {

    private DictQuery dictQuery;

    public DictSpecification(DictQuery dictQuery){
        this.dictQuery = dictQuery;
    }
    @Override
    public Predicate toPredicate(Root<DictEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
        if(dictQuery!=null){
            if(dictQuery.getId()!=null){
                Predicate p = criteriaBuilder.equal(root.get("id").as(Integer.class),dictQuery.getId());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getCreateBy()!=null){
                Predicate p = criteriaBuilder.equal(root.get("createBy").as(Integer.class),dictQuery.getCreateBy());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getDelFlag()!=null){
                Predicate p = criteriaBuilder.equal(root.get("delFlag").as(String.class),dictQuery.getDelFlag());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getDescription()!=null){
                Predicate p = criteriaBuilder.equal(root.get("description").as(String.class),dictQuery.getDescription());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getName()!=null){
                Predicate p = criteriaBuilder.equal(root.get("name").as(String.class),dictQuery.getName());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getRemarks()!=null){
                Predicate p = criteriaBuilder.equal(root.get("remarks").as(String.class),dictQuery.getRemarks());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getType()!=null){
                Predicate p = criteriaBuilder.equal(root.get("type").as(String.class),dictQuery.getType());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getSort()!=null){
                Predicate p = criteriaBuilder.equal(root.get("sort").as(BigDecimal.class),dictQuery.getSort());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getValue()!=null){
                Predicate p = criteriaBuilder.equal(root.get("value").as(String.class),dictQuery.getValue());
                query.where(criteriaBuilder.and(p));
            }
            if(dictQuery.getUpdateBy()!=null){
                Predicate p = criteriaBuilder.equal(root.get("updateBy").as(Integer.class),dictQuery.getUpdateBy());
                query.where(criteriaBuilder.and(p));
            }
            return query.getRestriction();


        }else{
            //不需要加条件返回null
            return null;
        }
    }


}
